package com.planet.api;

import com.planet.facade.HouseholdBiz;
import com.planet.query.HouseholdQuery;
import com.planet.query.UserQuery;
import com.planet.utils.AssertUtils;
import com.planet.utils.UtilTools;
import com.planet.utils.global.ExceptionHandle;
import com.planet.utils.global.Result;
import com.planet.utils.global.ResultUtil;
import com.planet.vo.HouseholdVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by jiayazi on 17/11/14.
 */
@Api("住户信息")
@RestController
@RequestMapping("nb/api/host")
public class HostAPI {

    @Autowired
    private ExceptionHandle exceptionHandle;
    @Autowired
    HouseholdBiz householdBiz;

    /**
     * 插入租户
     * @param householdQuery 用户Id
     * @return
     */
    @GetMapping("/insertHost")
    @ApiOperation(value = "插入租户")
    public Result insertHost(@ApiParam("租户信息") HouseholdQuery householdQuery){

        AssertUtils.isNotNull(householdQuery.getName(),"姓名不能为空");
        AssertUtils.isNotNull(householdQuery.getPhone(),"手机号不能为空");
        //默认结果是成功的
        Result result = ResultUtil.success();
        try {
            HouseholdVO householdVO = householdBiz.selectByPhone(householdQuery.getPhone());
            if (StringUtils.isNotBlank(householdVO.getPhone())){
                result = ResultUtil.error(1,"该租户已经存在");
                return result;
            }

            boolean registerState = householdBiz.insertHost(householdQuery);
            if (registerState){
                result = ResultUtil.success(0,"租户插入成功");
                return result;
            }

        }catch (Exception e){
            //调用异常工具类，统一处理异常
            result =  exceptionHandle.exceptionGet(e);
        }
        return result;
    }


    /**
     * 住户列表
     * @param
     * @return
     */
    @GetMapping("/householdList")
    @ApiOperation(value = "住户列表")
    public Result householdList(String pageSize, String pageIndex){

        //默认结果是成功的
        Result result = ResultUtil.success();
        try {
            List<HouseholdVO> householdList = householdBiz.getHostList();
            result =  ResultUtil.success(householdList);

        }catch (Exception e){
            //调用异常工具类，统一处理异常
            result =  exceptionHandle.exceptionGet(e);
        }
        return result;
    }

//
//
//    /**
//     * 住户详情
//     * @param
//     * @return
//     */
//    @GetMapping("/householdDetail")
//    @ApiOperation(value = "住户详情")
//    public Result householdList(String pageSize,String pageIndex){
//
//        //默认结果是成功的
//        Result result = ResultUtil.success();
//        try {
//            YhcclrVO yhcclrVO = yhcclrBiz.getCheckInfo(tarchId);
//            result =  ResultUtil.success(yhcclrVO);
//
//        }catch (Exception e){
//            //调用异常工具类，统一处理异常
//            result =  exceptionHandle.exceptionGet(e);
//        }
//        return result;
//    }

}


